using System;
using System.Collections.Generic;
using System.Linq;
namespace ServiceStack
{
	[AttributeUsage]
	public class RestrictAttribute : AttributeBase
	{
		public bool VisibleInternalOnly
		{
			get
			{
				return this.CanShowTo(RequestAttributes.InternalNetworkAccess);
			}
			set
			{
				if (!value)
				{
					throw new Exception("Only true allowed");
				}
				this.VisibilityTo = RequestAttributes.InternalNetworkAccess.ToAllowedFlagsSet();
			}
		}
		public bool VisibleLocalhostOnly
		{
			get
			{
				return this.CanShowTo(RequestAttributes.Localhost);
			}
			set
			{
				if (!value)
				{
					throw new Exception("Only true allowed");
				}
				this.VisibilityTo = RequestAttributes.Localhost.ToAllowedFlagsSet();
			}
		}
		public bool LocalhostOnly
		{
			get
			{
				return this.HasAccessTo(RequestAttributes.Localhost) && this.CanShowTo(RequestAttributes.Localhost);
			}
			set
			{
				if (!value)
				{
					throw new Exception("Only true allowed");
				}
				this.AccessTo = RequestAttributes.Localhost.ToAllowedFlagsSet();
				this.VisibilityTo = RequestAttributes.Localhost.ToAllowedFlagsSet();
			}
		}
		public bool InternalOnly
		{
			get
			{
				return this.HasAccessTo(RequestAttributes.InternalNetworkAccess) && this.CanShowTo(RequestAttributes.InternalNetworkAccess);
			}
			set
			{
				if (!value)
				{
					throw new Exception("Only true allowed");
				}
				this.AccessTo = RequestAttributes.InternalNetworkAccess.ToAllowedFlagsSet();
				this.VisibilityTo = RequestAttributes.InternalNetworkAccess.ToAllowedFlagsSet();
			}
		}
		public bool ExternalOnly
		{
			get
			{
				return this.HasAccessTo(RequestAttributes.External) && this.CanShowTo(RequestAttributes.External);
			}
			set
			{
				if (!value)
				{
					throw new Exception("Only true allowed");
				}
				this.AccessTo = RequestAttributes.External.ToAllowedFlagsSet();
				this.VisibilityTo = RequestAttributes.External.ToAllowedFlagsSet();
			}
		}
		public RequestAttributes AccessTo
		{
			get
			{
				if (this.AccessibleToAny.Length != 0)
				{
					return this.AccessibleToAny[0];
				}
				return RequestAttributes.Any;
			}
			set
			{
				this.AccessibleToAny = new RequestAttributes[]
				{
					value
				};
			}
		}
		public RequestAttributes[] AccessibleToAny
		{
			get;
			private set;
		}
		public RequestAttributes VisibilityTo
		{
			get
			{
				if (this.VisibleToAny.Length != 0)
				{
					return this.VisibleToAny[0];
				}
				return RequestAttributes.Any;
			}
			set
			{
				this.VisibleToAny = new RequestAttributes[]
				{
					value
				};
			}
		}
		public RequestAttributes[] VisibleToAny
		{
			get;
			private set;
		}
		public bool HasNoAccessRestrictions
		{
			get
			{
				return this.AccessTo == RequestAttributes.Any;
			}
		}
		public bool HasNoVisibilityRestrictions
		{
			get
			{
				return this.VisibilityTo == RequestAttributes.Any;
			}
		}
		public RestrictAttribute()
		{
			this.AccessTo = RequestAttributes.Any;
			this.VisibilityTo = RequestAttributes.Any;
		}
		public RestrictAttribute(params RequestAttributes[] restrictAccessAndVisibilityToScenarios)
		{
			this.AccessibleToAny = RestrictAttribute.ToAllowedFlagsSet(restrictAccessAndVisibilityToScenarios);
			this.VisibleToAny = RestrictAttribute.ToAllowedFlagsSet(restrictAccessAndVisibilityToScenarios);
		}
		public RestrictAttribute(RequestAttributes[] allowedAccessScenarios, RequestAttributes[] visibleToScenarios) : this()
		{
			this.AccessibleToAny = RestrictAttribute.ToAllowedFlagsSet(allowedAccessScenarios);
			this.VisibleToAny = RestrictAttribute.ToAllowedFlagsSet(visibleToScenarios);
		}
		private static RequestAttributes[] ToAllowedFlagsSet(RequestAttributes[] restrictToAny)
		{
			if (restrictToAny.Length == 0)
			{
				return new RequestAttributes[]
				{
					RequestAttributes.Any
				};
			}
			List<RequestAttributes> list = new List<RequestAttributes>();
			for (int i = 0; i < restrictToAny.Length; i++)
			{
				RequestAttributes requestAttributes = restrictToAny[i].ToAllowedFlagsSet();
				list.Add(requestAttributes);
			}
			return list.ToArray();
		}
		public bool CanShowTo(RequestAttributes restrictions)
		{
			return Enumerable.Any<RequestAttributes>(this.VisibleToAny, (RequestAttributes scenario) => (restrictions & scenario) == restrictions);
		}
		public bool HasAccessTo(RequestAttributes restrictions)
		{
			return Enumerable.Any<RequestAttributes>(this.AccessibleToAny, (RequestAttributes scenario) => (restrictions & scenario) == restrictions);
		}
	}
}
